package com.mayikt.controller;

import com.mayikt.req.CategoryQueryReq;
import com.mayikt.req.CategorySaveReq;
import com.mayikt.resp.CommonResp;
import com.mayikt.resp.CategoryQueryResp;
import com.mayikt.resp.PageResp;
import com.mayikt.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/list")
    public CommonResp list(@Valid CategoryQueryReq categoryQueryReq) {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        PageResp<CategoryQueryResp> pageResp = categoryService.list(categoryQueryReq);
        resp.setData(pageResp);
        return resp;
    }

    @GetMapping("/all")
    public CommonResp all() {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> list = categoryService.all();
        resp.setData(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq categorySaveReq) {
        CommonResp resp = new CommonResp<>();
        int result = categoryService.save(categorySaveReq);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        int result = categoryService.delete(id);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }
}
